package com.syh.jn.scm.domain.operation.entity.financial;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.operation.repository.financial.FinancialContactReadRepository;
import com.syh.jn.scm.dto.operation.financial.FinancialMto;
import com.syh.jn.scm.dto.operation.financial.FinancialSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 运营端金融机构聚合
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Financial extends AbstractSimpleAggregate<Long> {
  /**
   * 地址省 市 区
   */
  private String area;
  /**
   * 单位注册地址
   */
  private String address;
  /**
   * 地区编号
   */
  private String areaCode;
  /**
   * 单位名称
   */
  private String name;
  /**
   * 状态: -1停用, 1可用
   */
  private Short status;
  /**
   * 对公电子邮件
   */
  private String email;
  /**
   * 公司联系电话
   */
  private String tel;
  /**
   * 地址明细
   */
  private List<FinancialAddress> addressItem;

  public Financial() {
  }

  public Financial(FinancialSto sto) {
    ExtAssert.notNull(sto,"对象为空");
    ValidationUtils.validate(sto);
    ExtBeanUtils.copyProperties(sto,this);
    this.initDefault();
    //启用
    this.enabled();
    //添加明细
    this.addressItem = new ArrayList<>();
    sto.getAddressItem().stream().forEach(addressSto-> {
      this.addItem(new FinancialAddress(addressSto));
    });
  }

  /**
   * 修改金融结构
   * @param mto
   */
  public void modify(FinancialMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this,false);
    this.addressItem = new ArrayList<>();
    mto.getAddressItem().stream().forEach(addressMto->{
      this.addItem(new FinancialAddress(addressMto));
    });
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 添加子项
   * @param address
   */
  private Financial addItem(FinancialAddress address) {
    this.addressItem.add(address);
    return this;
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = 1;
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = -1;
  }

  /**
   * 删除
   */
  public void destroy(Long id) {
    ExtAssert.notNull(id,"id为空");
    FinancialContactReadRepository financialQuery = (FinancialContactReadRepository) SpringContextUtils.getBean("financialContactRepositoryImpl");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("businessEntityId", id));
    List<FinancialContact> list = financialQuery.getList(wrapper);
    if(list != null && list.size() > 0){
      throw new BusinessException("该金融机构关联了供应商不可以删除");
    }
    if(this.status.equals(Short.valueOf("1"))){
      throw new BusinessException("该金融机构是启用状态,不可以删除");
    }
  }

  /**
   * 金融机构不存在
   *
   * @param financial
   */
  public void check(Financial financial) {
    if(financial == null){
      throw new BusinessException("金融机构不存在");
    }
  }
}
